package com.oragraph.data.group.impl;

import java.util.Vector;

import com.oragraph.data.Node;
import com.oragraph.data.group.Group;
import com.oragraph.data.group.GroupFactory;
import com.oragraph.data.group.GroupTypeManager;
import com.oragraph.data.instance.Instance;
import com.oragraph.data.instance.InstanceFactory;
import com.oragraph.data.instance.InstanceTypeManager;
import com.oragraph.data.sourcedata.DummySourceData;

public class IPByUserGroupFactory implements GroupFactory {
	
	private static GroupFactory _singleton;
	
	static {
		_singleton=new IPByUserGroupFactory();
		GroupTypeManager.registerGroupFactory("USER", _singleton);
	}

	@Override
	public Group createByID(String ID) {
		BasicGroup result=new BasicGroup(ID, getName());
		result.setGroupFactory(this);
		return result;
	}

	@Override
	public Vector<Node> getMyChildren(Group group) {
		// TODO
		String user=group.getImmutableProperties().get("ID");
		InstanceFactory factory=InstanceTypeManager.getInstanceFactory("IP");
		Vector<Node> result=new Vector<Node>();
		
		for (int i=0; i<DummySourceData.IPs.size(); i++) {
			String IP=DummySourceData.IPs.elementAt(i);
			if (DummySourceData.match(user, IP)) {
				Instance instanceToAdd=factory.createByID(IP);
				if (GroupFactoryHelper.passesFilter(group, instanceToAdd)) {
					result.add(instanceToAdd);
				}
			}
		}
		
		return result;
	}

	@Override
	public String getName() {
		return "IPBYUSER";
	}


}
